/********************************** (C) COPYRIGHT *******************************
* File Name          : ch32v30x_it.c
* Author             : WCH
* Version            : V1.0.0
* Date               : 2024/03/06
* Description        : Main Interrupt Service Routines.
*********************************************************************************
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* Attention: This software (modified or not) and binary are used for 
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/
#include "ch32v30x_it.h"

#include "mydefine.h"

void NMI_Handler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void HardFault_Handler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void EXTI15_10_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
/*********************************************************************
 * @fn      NMI_Handler
 *
 * @brief   This function handles NMI exception.
 *
 * @return  none
 */
void NMI_Handler(void)
{
  while (1)
  {
  }
}

/*********************************************************************
 * @fn      HardFault_Handler
 *
 * @brief   This function handles Hard Fault exception.
 *
 * @return  none
 */
void HardFault_Handler(void)
{
  NVIC_SystemReset();
  while (1)
  {
  }
}

/*********************************************************************
 * @fn      EXTI0_IRQHandler
 *
 * @brief   This function handles EXTI0 Handler.
 *
 * @return  none
 */
void EXTI15_10_IRQHandler(void) {
    if (EXTI_GetITStatus(EXTI_Line13) != RESET) {
//    my_printf("Run at EXTI\r\n");
        uint8_t temp;
        //max30102_irq_flag = 1;
        temp = max30102_Bus_Read(REG_INTR_STATUS_1);
        //    if ( temp&0x40 )
        //        my_printf ( "0x40\n" );
        if (temp & 0x80) {
            max30102_Bus_Write(REG_FIFO_WR_PTR, 0x00); //0X04: FIFO_WR_PTR[4:0],recommend to clear first
            max30102_Bus_Write(REG_OVF_COUNTER, 0x00); //0X05: OVF_COUNTER[4:0],recommend to clear first
            max30102_Bus_Write(REG_FIFO_RD_PTR, 0x00); //0X06: FIFO_RD_PTR[4:0],recommend to clear first
        }
        EXTI_ClearITPendingBit(EXTI_Line13); /* Clear Flag */
    }
}

